package com.mls.guardian.appst.common;

import com.mls.guardian.config.AppSTConfig;
import com.mls.guardian.dataobj.BackupModuleObj;
import com.mls.guardian.dataobj.BackupProjectObj;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by pengrenfang on 15/11/11.
 */
public class BackupUtil {
    private static final String DATE_FORMAT = "yyyyMMdd_HH-mm-ss-SSS";
    private static BackupUtil instance;
    private AppSTConfig appSTConfig;

    private BackupUtil(AppSTConfig appSTConfig) {
        this.appSTConfig = appSTConfig;
    }

    public synchronized static BackupUtil getInstance(AppSTConfig appSTConfig) {
        if (BackupUtil.instance == null) {
            BackupUtil.instance = new BackupUtil(appSTConfig);
        }

        return BackupUtil.instance;
    }

    public List<BackupProjectObj> getBackup() throws Exception {
        return this.getBackup("", "");
    }

    public List<BackupProjectObj> getBackup(String projectName) throws Exception {
        return this.getBackup(projectName, "");
    }

    public List<BackupProjectObj> getBackup(String projectName, String versionTag) throws Exception {
        List<BackupProjectObj> rtnList = new ArrayList<BackupProjectObj>();
        String backupDest = this.appSTConfig.getBackupDest();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);

        File baseFile = new File(backupDest);
        if (baseFile != null && baseFile.exists() && baseFile.isDirectory()) {
            for (File fileProject : baseFile.listFiles()) {
                if (fileProject != null && fileProject.exists() && fileProject.isDirectory()) {
                    //项目名称
                    String tempProjectName = fileProject.getName();
                    if (projectName != null && projectName.length() > 0 && false == projectName.equals(tempProjectName)) {
                        continue;
                    }

                    for (File fileVersionTag : fileProject.listFiles()) {
                        if (fileVersionTag != null && fileVersionTag.exists() && fileVersionTag.isDirectory()) {
                            //备份文件夹
                            String tempVersionTag = fileVersionTag.getName();
                            if (versionTag != null && versionTag.length() > 0 && false == versionTag.equals(tempVersionTag)) {
                                continue;
                            }

                            BackupProjectObj itemProject = new BackupProjectObj();
                            itemProject.setProjectName(tempProjectName);
                            itemProject.setVersionTag(tempVersionTag);

                            List<BackupModuleObj> backupModuleObjList = new ArrayList<BackupModuleObj>();
                            for (File fileModule : fileVersionTag.listFiles()) {
                                if (fileModule != null && fileModule.exists() && fileModule.isFile()) {
                                    backupModuleObjList.add(new BackupModuleObj(fileModule.getName(), simpleDateFormat.format(fileModule.lastModified())));
                                }
                            }
                            itemProject.setModuleObjList(backupModuleObjList);

                            rtnList.add(itemProject);
                        }
                    }
                }

            }
        }

        return rtnList;
    }

}
